import { _decorator, Component, v3, RigidBody, Vec3, SkeletalAnimation, Animation, BoxCollider, AnimationState, Collider, ICollisionEvent, ColliderComponent } from 'cc';
import { CharacterState } from './PlayerController';
import { GameManager } from './GameManager';
const { ccclass, property } = _decorator;

const v3_0 = v3();
const velocity = v3();

export enum CharacterFace {
    高兴,
    悲伤,
    愤怒,
    挑衅,
    痛苦,
    非常愤怒,
    搞怪
}

@ccclass('CharacterController')
export class CharacterController extends Component {
    rigidBody: RigidBody;
    collider: ColliderComponent;
    animation: SkeletalAnimation = null;
    state: string = "";

    IsDead: boolean = false;

    negativeFace: number[] = [1, 4];
    positiveFace: number[] = [0, 2, 3, 5, 6];

    protected onLoad(): void {
        this.rigidBody = this.node.getComponent(RigidBody);
        this.collider = this.node.getComponent(ColliderComponent);

        this.animation = this.node.children[0].getComponent(SkeletalAnimation);
        this.animation.on(Animation.EventType.FINISHED, this.onAnimationFinishedEvent, this);
    }

    // protected onCollisionEnter(event: ICollisionEvent) {
    // }

    update(dt: number) {
        this.rigidBody.getLinearVelocity(velocity);
        velocity.z = 0;
        this.rigidBody.setLinearVelocity(velocity);
    }

    //#region 动画

    PlayAni(state: string) {
        if (this.IsDead) return;
        if (this.state == state) return;
        this.state = state;
        this.animation.crossFade(state);
    }

    onAnimationFinishedEvent(type: Animation.EventType, state: AnimationState) { }

    AnimFrameEvent(param: string) { }

    //#endregion

    protected onEnable(): void {
        // this.collider.on('onCollisionEnter', this.onCollisionEnter, this);

        // this.attackCollider.on('onTriggerEnter', this.onAttackTriggerEnter, this);
        // this.attackCollider.on('onTriggerExit', this.onAttackTriggerExit, this);
    }

    protected onDisable(): void {
        // this.collider.off('onCollisionEnter', this.onCollisionEnter, this);

        // this.attackCollider.off('onTriggerEnter', this.onAttackTriggerEnter, this);
        // this.attackCollider.off('onTriggerExit', this.onAttackTriggerExit, this);
    }
}